const sqlite3 = require("sqlite3")
const path = require("node:path")
const { app } = require('electron')

const configPath = app && app.isPackaged 
    ? path.join(process.resourcesPath, 'config.json') 
    : path.join(__dirname, "../../config/config.json")
const coreConfig = require(configPath)

const defaultDbPath = app && app.isPackaged 
    ? path.join(process.resourcesPath, 'app.db') 
    : path.join(__dirname, "../../config/app.db")

const dbPath = coreConfig.dbPath ? path.join(coreConfig.dbPath) : defaultDbPath
const db = new sqlite3.cached.Database(dbPath);

exports.coreConfig  = { ...coreConfig, dbPath: dbPath }
exports.defaultDbPath  = defaultDbPath
exports.dbPath  = dbPath
exports.db = db

function initSql() {
    var initCompanySql  = "CREATE TABLE if not exists tb_company(id integer primary key autoincrement, name varchar not null, orders integer default 10)";
    var initProjectSql  = "CREATE TABLE if not exists tb_project(id integer primary key autoincrement, name varchar not null, cid integer not null, orders integer default 10)";
    var initItemSql     = "CREATE TABLE if not exists tb_item(id integer primary key autoincrement, name varchar, url varchar, content text, pid integer not null, orders integer default 10)";
    var initBookmarkSql = "CREATE TABLE if not exists tb_bookmark(id integer primary key autoincrement, parent_id integer not null default 0, type tinyint not null default 1, title varchar(100), url varchar(1024), icon text,orders tinyint default 10, create_time datetime default current_timestamp, update_time datetime default current_timestamp, hidden tinyint default 1)"
    var initUserkSql    = "CREATE TABLE if not exists tb_user(id integer primary key autoincrement, username varchar(100), password varchar(1024), secret_key varchar(1024), create_time datetime default current_timestamp, update_time datetime default current_timestamp)"

    db.serialize(function(){
        db.run(initCompanySql, (err) => {
            console.log(err ? err.message : "init tb_company success")
        })

        db.run(initProjectSql, (err) => {
             console.log(err ? err.message : "init tb_project success")
        })

        db.run(initItemSql, (err) => {
            console.log(err ? err.message : "init tb_item success")
        })

        db.run(initBookmarkSql, (err) => {
            console.log(err ? err.message : "init tb_bookmark success")
        })

        db.run(initUserkSql, (err) => {
            console.log(err ? err.message : "init tb_user success")
        })
    })
}

function clearDb() {
    db.serialize(function(){
        db.run("drop table if exists tb_company");
        db.run("drop table if exists tb_project");
        db.run("drop table if exists tb_item");
        db.run("drop table if exists tb_bookmark");
        db.run("drop table if exists tb_user");
    })
}

function InitData() {
    db.serialize(function() {
        db.run("insert into tb_company (name) values ('公司甲')",  err => {
            console.log(err ? err.message : "Insert into tb_company success")
        })
    
        db.run("insert into tb_project (name, cid) values ('工程1', 1)", err => {
            console.log(err ? err.message : "Insert into tb_project success")
        })

        db.run("insert into tb_item (name, url, pid) values ('项目1', 'https://baidu.com', 1)", err => {
            console.log(err ? err.message : "Insert into tb_item success")
        })
    })
}


exports.initSql = initSql
exports.clearDb = clearDb
exports.InitData = InitData


